/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.capex.javaee.sockets;

import java.io.BufferedReader;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 *
 * @author Acer
 */
public class TcpServer {
    public static void main(String[] args) {
        try{
            System.out.println("Starting TCP Echo Server");
            String clientMessage;
            String serverReply;
            ServerSocket tcpServerSocket = new ServerSocket(6789);
            
            while(true){
                Socket connectionSocket = tcpServerSocket.accept();
                //Input Stream to Read what the Client Writes
                BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
                
                //Output Stream to Write to the Client
                DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());

                clientMessage = inFromClient.readLine();

                System.out.println("From Client: "+clientMessage);
                serverReply = "Ok, I recieved your Message\n";

                outToClient.writeBytes(serverReply);
                
                inFromClient.close();
                outToClient.close();
            }
        }catch(Exception ex){

        }
    }
}
